package com.jing.ads.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Toast;

import com.jing.ads.R;
import com.jing.ads.application.MyApplication;
import com.jing.ads.view.HandlerView;

public class ActivityTest extends Activity {

	HandlerView myFV;
	WindowManager wm;
	WindowManager.LayoutParams wmParams = null;

	private float mTouchStartX;

	private float mTouchStartY;

	private float x;

	private float y;

	int width, height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View v = new View(this);
		v.setBackgroundColor(Color.WHITE);
		v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		setContentView(v);
		createView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		wm.removeView(myFV);
	}

	float rx, ry;

	private void createView() {
		myFV = new HandlerView(this);

		wm = (WindowManager) getApplicationContext().getSystemService(
				WINDOW_SERVICE);
		width = wm.getDefaultDisplay().getWidth();
		height = wm.getDefaultDisplay().getHeight();
		int offset = wm.getDefaultDisplay().getHeight() / 5;
		wmParams = ((MyApplication) getApplication()).getMywmParams();
		wmParams.type = 2002;
		// wmParams.alpha = 100;
		wmParams.format = PixelFormat.TRANSPARENT;
		wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		// wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL;
		int wO = (58 * width) / 480;
		wmParams.gravity = Gravity.LEFT | Gravity.TOP;
		wmParams.x = 0;
		wmParams.y = offset - 10;
		wmParams.width = wO;
		wmParams.height = wO;
		wm.addView(myFV, wmParams);
		myFV/* .findViewById(R.id.mask_point) */.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});
		myFV.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				x = event.getRawX();
				y = event.getRawY();
				switch (action) {
				case MotionEvent.ACTION_DOWN:
					mTouchStartX = event.getX();
					mTouchStartY = event.getY();
					rx = event.getX();
					ry = event.getY();
					break;

				case MotionEvent.ACTION_MOVE:
					float zx = event.getX();
					float zy = event.getY();
					float mzx = Math.abs(rx - zx);
					float mzy = Math.abs(ry - zy);
					if (mzx >= 10 || mzy >= 10) {
						updateViewPosition(false);
					}
					break;

				case MotionEvent.ACTION_UP:
					float zzx = event.getX();
					float zzy = event.getY();
					float mmzx = Math.abs(rx - zzx);
					float mmzy = Math.abs(ry - zzy);
					if (mmzx >= 10 || mmzy >= 10) {
						updateViewPosition(true);
					} else {
					}
					rx = ry = 0;
					mTouchStartX = mTouchStartY = 0;
					break;

				}
				return true;
			}
		});
	}

	private void updateViewPosition(boolean up) {
		int xo = (int) (x - mTouchStartX);
		int yo = (int) (y - mTouchStartY);
		wmParams.x = xo;
		wmParams.y = yo;
		if (up) {
			wmParams.x = (x >= width / 2) ? width : 0;
			wmParams.y = (int) ((y >= height - 200) ? height - 200 : y);
		}
		wm.updateViewLayout(myFV, wmParams);
	}

}
